Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add migrate_cri #1519

Merged
merged 1 commit into from
Sep 28, 2022
Merged

add migrate_cri #1519

merged 1 commit into from
Sep 28, 2022

Conversation

zt1046656665
Copy link

What type of PR is this?

migrate

What this PR does / why we need it:

we add migrate_cri

Which issue(s) this PR fixes:

Fixes #

Special notes for reviewers:

Does this PR introduced a user-facing change?


Additional documentation, usage docs, etc.:


@ks-ci-bot ks-ci-bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Sep 22, 2022
@zhouqiu0103
Copy link

zhouqiu0103 commented Sep 23, 2022

Could you please provide documentation or description of the newly added features? Such as the purpose of the command, examples of command usage.The documentation can be referenced to commands docs

@zt1046656665
Copy link
Author

Could you please provide documentation or description of the newly added features? Such as the purpose of the command, examples of command usage.The documentation can be referenced to commands docs

ok, i will add it soon,thanks

loaderType = common.AllInOne
}

fmt.Println("MigrateCriPipeline called")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why print the information in this place?

Comment on lines 326 to 333
if p.KubeConf.Arg.Type == common.Docker && p.KubeConf.Arg.Role == common.Worker {
p.Tasks = MigrateToDocker(p)
} else if p.KubeConf.Arg.Type == common.Docker && p.KubeConf.Arg.Role == common.Master {
p.Tasks = MigrateMToDocker(p)
} else if p.KubeConf.Arg.Type == common.Conatinerd && p.KubeConf.Arg.Role == common.Worker {
p.Tasks = MigrateToContainerd(p)
} else if p.KubeConf.Arg.Type == common.Conatinerd && p.KubeConf.Arg.Role == common.Master {
p.Tasks = MigrateMToContainerd(p)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommended use:

if p.KubeConf.Arg.Type == common.Docker {
        if p.KubeConf.Arg.Role == common.Worker {
		// xxx
	}
        if p.KubeConf.Arg.Role == common.Master {
		// xxx
	}
}
if p.KubeConf.Arg.Type == common.Conatinerd {
        if p.KubeConf.Arg.Role == common.Worker {
		// xxx
	}
        if p.KubeConf.Arg.Role == common.Master {
		// xxx
	}
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is better, I will optimize it, thank you

Copy link
Collaborator

@24sama 24sama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contributions! There are some comments, PTAL.

@@ -0,0 +1,46 @@
/*
Copyright 2020 The KubeSphere Authors.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2020 -> 2022

Comment on lines 73 to 82
cmd.Flags().StringVarP(&o.Role, "role", "", "worker", "Role to migrate")
cmd.Flags().StringVarP(&o.Type, "type", "", "docker", "Type to migrate")
cmd.Flags().StringVarP(&o.ClusterCfgFile, "filename", "f", "", "Path to a configuration file")
cmd.Flags().StringVarP(&o.Kubernetes, "with-kubernetes", "", "", "Specify a supported version of kubernetes")
cmd.Flags().StringVarP(&o.DownloadCmd, "download-cmd", "", "curl -L -o %s %s",
`The user defined command to download the necessary binary files. The first param '%s' is output path, the second param '%s', is the URL`)
cmd.Flags().StringVarP(&o.Artifact, "artifact", "a", "", "Path to a KubeKey artifact")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest using Complete() and Validate() methods to check the user input.

@@ -145,3 +146,76 @@ func KubernetesArtifactBinariesDownload(manifest *common.ArtifactManifest, path,

return nil
}

// CriDownloadHTTP defines the kubernetes' binaries that need to be downloaded in advance and downloads them.
func DockerDownloadHTTP(kubeConf *common.KubeConf, path, arch string, pipelineCache *cache.Cache) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think DockerDownloadHTTP() and ContainerdDownloadHTTP() can be modified as one function, which is can receive variable files.KubeBinary{}.
For example:

func DownloadBinaries(kubeConf *common.KubeConf, pipelineCache *cache.Cache, binaries ...*files.KubeBinary) {
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right,I will optimize it,soon

}

func (d *MigrateSelfNodeTocontainerd) Execute(runtime connector.Runtime) error {
nodeName, _ := runtime.GetRunner().SudoCmd("hostname", true)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened if the command hostname reported an error?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will optimize it,soon

func (d *DockerDownload) Execute(runtime connector.Runtime) error {
cfg := d.KubeConf.Cluster

// var kubeVersion string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove useless codes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will optimize it,soon

loaderType = common.AllInOne
}

fmt.Println("MigrateCriPipeline called")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will optimize it,soon

}

if err := MigrateCriPipeline(runtime); err != nil {
fmt.Println(err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will optimize it,soon

fmt.Println("MigrateCriPipeline called")
runtime, err := common.NewKubeRuntime(loaderType, args)
if err != nil {
fmt.Println(err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line, which will cause print error message twice.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes,I will optimize it,soon

Comment on lines 383 to 397
nodeName, _ := runtime.GetRunner().SudoCmd("hostname", true)
if _, err := runtime.GetRunner().SudoCmd(fmt.Sprintf("/usr/local/bin/kubectl cordon %s ", nodeName), true); err != nil {
return errors.Wrap(err, fmt.Sprintf("cordon the node: %s failed", nodeName))
}
if _, err := runtime.GetRunner().SudoCmd(fmt.Sprintf(
"/usr/local/bin/kubectl drain %s --delete-emptydir-data --ignore-daemonsets --timeout=2m --force", nodeName),
true); err != nil {
return errors.Wrap(err, fmt.Sprintf("drain the node: %s failed", nodeName))
}
// if _, err := runtime.GetRunner().SudoCmd("systemctl stop kubelet", false); err != nil {
// return errors.Wrap(errors.WithStack(err), fmt.Sprintf("stop kubelet failed: %s", nodeName))
// }
if err := UninstallContainerTasks(runtime, d.KubeAction); err != nil {
return errors.Wrap(errors.WithStack(err), fmt.Sprintf("Uninstall ContainerTasks failed: %s", nodeName))
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see these codes are duplicated with MigrateSelfNodeTocontainerd, so I suggest modifying them as a new task.
Just an advice, maybe after that the MigrateSelfNodeToDocker can become to:

func (d *MigrateSelfNodeToDocker) Execute(runtime connector.Runtime) error {
  CordonTask
  DrainTask
  uninstallTask
  MigrateTask
  RestartTask
  UnCordonTask
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, a good advice, I will optimize it,soon

Comment on lines 326 to 334
if p.KubeConf.Arg.Type == common.Docker && p.KubeConf.Arg.Role == common.Worker {
p.Tasks = MigrateToDocker(p)
} else if p.KubeConf.Arg.Type == common.Docker && p.KubeConf.Arg.Role == common.Master {
p.Tasks = MigrateMToDocker(p)
} else if p.KubeConf.Arg.Type == common.Conatinerd && p.KubeConf.Arg.Role == common.Worker {
p.Tasks = MigrateToContainerd(p)
} else if p.KubeConf.Arg.Type == common.Conatinerd && p.KubeConf.Arg.Role == common.Master {
p.Tasks = MigrateMToContainerd(p)
} else {
logger.Log.Fatalf("Unsupported container runtime: %s", strings.TrimSpace(p.KubeConf.Arg.Type))
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened if the flag role support this --role all? Add a new module?
Maybe we just need to add a new prepare to determine if the node should be skipped.

var action task.Action
switch type {
case docker:
  action =  new(MigrateSelfNodeToDocker)
case contained:
  action = new(MigrateSelfNodeToContainerd)
}

migrate = &task.RemoteTask{
		Name:     "MigrateToDocker",
		Desc:     "Migrate To Docker",
		Hosts:    p.Runtime.GetHostsByRole(common.k8s),
		Prepare:  new(RolePrepare),
		Action:   action,
		Parallel: false,
	}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok,I will optimize it,soon

}

}
if _, err := runtime.GetRunner().SudoCmd(fmt.Sprintf("/usr/local/bin/kubectl uncordon %s", nodeName), true); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this task need to execute twice uncordon commands?

Comment on lines 76 to 77
cmd.Flags().StringVarP(&o.Role, "role", "", "worker", "Role to migrate")
cmd.Flags().StringVarP(&o.Type, "type", "", "docker", "Type to migrate")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is recommended not to set the default value. And the description needs more detailed, for example, which roles can be specified, and which types can be specified.

Comment on lines 86 to 98
if o.Role == "" {
return errors.New("node Role can not be empty")
}
if o.Type == "" {
return errors.New("cri Type can not be empty")
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to check the user input value if it is valid, rather than only check if it is empty.


func (o *MigrateCriOptions) Validate() error {
if o.Role == "" || (o.Role != common.Worker && o.Role != common.Master && o.Role != "all") {
return errors.New("node Role should be valid ")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if o.Role == "" {
  return errors.New("The role can not be empty")
}
if o.Role != common.Worker && o.Role != common.Master && o.Role != "all" {
  return errors.Errorf("role %s is invalid", o.Role)
}

Signed-off-by: zhoutian <zhoutian@yunify.com>
@24sama
Copy link
Collaborator

24sama commented Sep 28, 2022

/lgtm
/approve

@ks-ci-bot ks-ci-bot added the lgtm Indicates that a PR is ready to be merged. label Sep 28, 2022
@ks-ci-bot
Copy link
Collaborator

LGTM label has been added.

Git tree hash: 9df097d779cd142634bff844c6570feedc507a50

@ks-ci-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: 24sama, zt1046656665

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ks-ci-bot ks-ci-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 28, 2022
@ks-ci-bot ks-ci-bot merged commit 68bd1dd into kubesphere:master Sep 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants